package com.aaa.controller;

import com.aaa.entity.T_Sd;
import com.aaa.service.T_ShidiService;
import com.aaa.util.ExcelUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: TT
 * @Date: 2019/3/30 10:38
 * @Version 1.0
 */
@Controller
@RequestMapping("/shidi/")
public class T_ShidiController {
    @Autowired
    T_ShidiService service;

    private List<T_Sd> field=null;
    @RequestMapping("html")
    public String html(){
        return "statistics/shidi";
    }

    @RequestMapping("selectPass")
    @ResponseBody
    public Map selectPass(@RequestBody Map map) {
        List<Map> list = service.selectAll(map);
        Integer total=service.getCount(map);
        Map value = new HashMap();
        value.put("total", total);
        value.put("data", list);
        return value;
    }
    @RequestMapping("data")
    @ResponseBody
    public int excel(@RequestBody Map map){
        field =new ArrayList<T_Sd>();
        List<Map> list= (List) map.get("xinxi");
        for (int i=0;i<list.size();i++){
            T_Sd T_Sd=new T_Sd();
            T_Sd.setName((String) list.get(i).get("name"));
            T_Sd.setSHtime((String) list.get(i).get("SHtime"));
            T_Sd.setInscore((Integer) list.get(i).get("inscore"));
            field.add(T_Sd);
            System.out.println(list.get(i));
        }
        return 1;
    }
    @RequestMapping("export")
    @ResponseBody
    public void export(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //获取数据
        List<T_Sd> list = field;
        //excel标题
        String[] title = {"单位名称", "分数", "考察时间"};

        //excel文件名
        String fileName = "实地考察信息表" + System.currentTimeMillis() + ".xls";
        //sheet名
        String sheetName = "考察信息表";
        String[][] content = new String[list.size()][];
        for (int i = 0; i < list.size(); i++) {
            content[i] = new String[title.length];
            T_Sd obj = list.get(i);
            content[i][0] = obj.getName();
            content[i][1] = String.valueOf(obj.getInscore());
            content[i][2] = obj.getSHtime();
        }
        //创建HSSFWorkbook
        HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(sheetName, title, content, null);

        //响应到客户端
        try {
            this.setResponseHeader(response, fileName);
            OutputStream os = response.getOutputStream();
            wb.write(os);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //发送响应流方法
    @SuppressWarnings("all")
    public void setResponseHeader(HttpServletResponse response, String fileName) {
        try {
            try {
                fileName = new String(fileName.getBytes(),"ISO8859-1");
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            response.setContentType("application/octet-stream;charset=ISO8859-1");
            response.setHeader("Content-Disposition", "attachment;filename="+ fileName);
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
